<?php

/**
 * Description of SupportController
 *
 * @author Francis Adu-Gyamfi <francis.adu-gyamfi@matrixdesigns.org>
 */
class SupportController extends AppController {
    //put your code here
    
    public $uses = array('User', 'Organisation', 'Member');
    
    public function beforeFilter() {
        parent::beforeFilter();
        
        $this->autoRender = false;
    }
    
    public function login() {
    
        $rules = array(
            'required' => 'username, password'
        );
        
        $data = $this->request->data;

        $validated = $this->_validateParams($rules, $data);

        if ($validated != "NO_ERROR") {
            return $this->_response($validated);
        }

        extract($data);
        
        $account = $this->User->attempt_login($username, $password);
        
        if( $account ) {
            return $this->_response(array('status' => 'success', 'message' => 'Login successful', 'account' => $account));
        }
        
        return $this->_response(array('status' => 'failed', 'message' => 'Login failed. Please try again'));
    }
    
    public function organisations() {
        $this->autoRender = false;
        
        $organisations = $this->Organisation->find('all', array(
            'order' => array('Organisation.created' => 'desc'),
            'recursive' => -1 
        ));
        
        foreach($organisations as &$organisation) {
            $organisation_id = $organisation['Organisation']['id'];
            $organisation['Organisation']['members'] = $this->Organisation->OrganisationMember->getTotalMembers($organisation_id);
            $organisation['Organisation']['categories'] = $this->Organisation->OrganisationMemberCategory->getTotal($organisation_id);
        }
        
        return $this->_response(array('status' => 'sucsess', 'data' => $organisations));
    }
    
    public function organisation_stats() {
        $organisation_id = isset($_POST['organisation_id']) ? $_POST['organisation_id'] : -1;

        /*$stats = array(
            'total_members' => $this->OrganisationMember->getTotalMembers($organisation_id),
            'total_males' => $this->OrganisationMember->getTotalMembersByGender($organisation_id, 'Male'),
            'total_females' => $this->OrganisationMember->getTotalMembersByGender($organisation_id, 'Female')
        );*/
        
        $stats = $this->Organisation->OrganisationMember->getStatsByCategory($organisation_id);

        return $this->_response($stats);
    }
    
    /*public function portNumbersAndEmails() {
        $members = $this->Member->find('all', array(
            'recursive' => -1
        ));
        
        foreach($members as $member) {
            
            $match = "/[,\/\|]+/";
            $set = array(
                'mobile' => preg_split( $match, $member['Member']['mobile_number'] ),
                'office' => preg_split( $match, $member['Member']['office_number'] ),
                'home' => preg_split( $match, $member['Member']['home_number'] ),
            );
            
            $numbers = array();
            
            foreach($set as $type => $group) {
                foreach($group as $number) {
                    if( $number != '' ) { 
                        $number = str_replace(array('-',' '), '', $number);
                        
                        if( $number[0] == '0' ) {
                            $number = "+233" . substr($number, 1);
                        }
                        
                        if( substr($number, 0, 3) == '233' ) {
                            $number = "+" . $number;
                        }
                        
                        $numbers[] = array(
                            'member_id' => $member['Member']['id'],
                            'type' => $type,
                            'number' => $number
                        );
                    }
                }
            }
            
            if( $numbers ) {
                $this->Member->MemberPhoneNumber->create();
                $this->Member->MemberPhoneNumber->saveAll($numbers);
            }
            
            if( $member['Member']['email'] != '' ) {
                $this->Member->MemberEmail->create();
                $this->Member->MemberEmail->save(array(
                    'member_id' => $member['Member']['id'],
                    'email' => $member['Member']['email']
                ));
            }
        }
    }*/
}
